﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using QuickGraph;
using GraphSharp;
using GraphSharp.Controls;
using System.IO;

namespace VirtualOperator
{
    /// <summary>
    /// Interaction logic for TopologyViewer.xaml
    /// </summary>
    /// 
    

    public partial class TopologyViewer : UserControl
    {
        private VirtualNetwork graph;
       
        public VirtualNetwork Graph
        {
            get { return graph; }
            set
            {
                graph = value;
                
            }
        }
        private ImageBrush nodeBrush = new ImageBrush();
        private ImageBrush sourceBrush = new ImageBrush();
        private ImageBrush sinkBrush = new ImageBrush();
        public bool LoadFalg { get; private set; }

        public TopologyViewer()
        {
      
            InitializeComponent();
            try
            {
                nodeBrush.ImageSource = new BitmapImage(new Uri("Pictures\\node.png", UriKind.Relative));

            }
            catch (FileNotFoundException) { return; }
               
        }

        public void LoadTopology(VirtualNetwork topology)
        {
            this.graph = topology;
            this.graphLayout.Graph = graph;
            this.DataContext = graph;

            this.SetupGraph();
        }

        public void ColorNetworkConnection(UserConnection nc)
        {
            EdgeControl ec;
            VirtualNetwork.Link bidirect;
            PaintAllLinks(Brushes.Gray);
            foreach (VirtualNetwork.Link link in nc.links)
            {
                if ((bidirect = graph.IsBidirectionalLink(link)) != null)
                {
                    ec = graphLayout.GetEdgeControl(bidirect);
                    ec.Foreground = Brushes.Red;
                }
                ec = graphLayout.GetEdgeControl(link);
                ec.Foreground = Brushes.Red;
                
            }
        }
        public void PaintAllLinks(Brush brush)
        {
            EdgeControl ec;
            foreach (VirtualNetwork.Link edge in graph.Edges)
            {
                ec = graphLayout.GetEdgeControl(edge);
                if (ec != null)
                {
   
                    ec.Foreground = brush;
                }
            }
        
        }

        private void SetupGraph()
        {
          
            VertexControl vs;
            EdgeControl ec;


            foreach(VirtualNetwork.Matrix node in graph.Vertices)
            {
                vs = graphLayout.GetVertexControl(node);
                if (vs != null)
                {
                    GraphCanvas.SetX(vs, node.Possition.X);
                    GraphCanvas.SetY(vs, node.Possition.Y);
                    vs.MouseDoubleClick += new MouseButtonEventHandler(TopologyLayout_NodeDoubleClick);
                    vs.LostMouseCapture += new MouseEventHandler(TopologyLayout_LostMouseCapture);
                    vs.Foreground = Brushes.Black;
                    vs.FontSize = 9;
                    vs.Width = 50;
                    vs.Height = 40;
                    if (node.Type == "switch" || node.Type == "matrix")
                        vs.Background = nodeBrush;
              

                   
                }
                LoadFalg = true;

            }

            foreach (VirtualNetwork.Link edge in graph.Edges)
            {
                ec = graphLayout.GetEdgeControl(edge);
                if (ec != null)
                {
                    ec.MouseDoubleClick += new MouseButtonEventHandler(TopologyLayout_LinkDoubleClick);
                    ec.Foreground = Brushes.Gray;
                }
            }


        }
        private bool open;

        void TopologyLayout_NodeDoubleClick(object sender, RoutedEventArgs e)
        {
            VertexControl vs = (VertexControl)sender;
            VirtualNetwork.Matrix matrix = (VirtualNetwork.Matrix)vs.Vertex;
            if(!matrix.Open)
            {
               matrix.Open = true;
               NodeDetails nodeDetails = new NodeDetails(matrix);
               nodeDetails.Show();
            }

        }

        void TopologyLayout_LinkDoubleClick(object sender, RoutedEventArgs e)
        {
            EdgeControl ec = (EdgeControl)(sender);
            VirtualNetwork.Link link  = (VirtualNetwork.Link)(ec.Edge);
            if (!link.Open)
            {
                link.Open = true;
                LinkDetails linkDetails = new LinkDetails(link);
                linkDetails.Show();
            }
        }

        void TopologyLayout_LostMouseCapture(object sender, MouseEventArgs e)
        {
            VertexControl vs = (VertexControl)sender;
            VirtualNetwork.Matrix node = (VirtualNetwork.Matrix)vs.Vertex;
            double x = GraphCanvas.GetX(vs);
            double y = GraphCanvas.GetY(vs);
            node.Possition.SetPossition(x, y);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
